/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.demo.entity.Simp1010012;
import org.springblade.modules.demo.wrapper.Simp1010012Wrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.demo.entity.Simp1010012;
import org.springblade.modules.demo.vo.Simp1010012VO;
import org.springblade.modules.demo.service.ISimp1010012Service;
import org.springblade.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 消费统计表（月报） 控制器
 *
 * @author BladeX
 * @since 2021-12-24
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("blade-demo/simp1010012")
@Api(value = "消费统计表（月报）", tags = "消费统计表（月报）接口")
public class Simp1010012Controller extends BladeController {

	private final ISimp1010012Service simp1010012Service;
	// 创建对象 因为需要保存上一次的值
	private Map<String,String > map;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入simp1010012")
	public R<Simp1010012VO> detail(Simp1010012 simp1010012) {
		Simp1010012 detail = simp1010012Service.getOne(Condition.getQueryWrapper(simp1010012));
		return R.data(Simp1010012Wrapper.build().entityVO(detail));
	}

	/**
	 * 分页 消费统计表（月报）
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入simp1010012")
	public R<IPage<Simp1010012VO>> list(@ApiIgnore @RequestParam Map<String, Object> simp1010012, Query query) {
		QueryWrapper<Simp1010012> queryWrapper;
		if (simp1010012.containsKey("baltime_datelt")&&simp1010012.containsKey("baltime_datege"))
		{
			for (String mp : simp1010012.keySet())
			{
				System.out.println("检车key:" + mp + ", value:" + simp1010012.get(mp)+";");
				if (mp.equals("baltime_datelt"))
				{
					//前端选择时间后 点击分页可能传空值 如果值正确可以去掉该判断
					if (simp1010012.get("baltime_datelt").toString().length()==6&&(simp1010012.get("baltime_datelt").toString().charAt(0)!='N'))
					{
						log.info("逻辑3："+simp1010012.get(mp).toString());
						map.put("baltime_datelt",simp1010012.get(mp).toString());
						log.info("逻辑测试3："+map);
					}
				}else if (mp.equals("baltime_datege"))
				{
					//前端选择时间后 点击分页可能传空值 如果值正确可以去掉该判断
					if (simp1010012.get("baltime_datege").toString().length()==6&&(simp1010012.get("baltime_datege").toString().charAt(0)!='N'))
					{
						map.put("baltime_datege", simp1010012.get(mp).toString());
						log.debug("");
					}
				}
			}
			//为了匹配框架,在前端写了处理日期字符串的逻辑。  框架里面这个传时间默认框架处理会导致类型不匹配 默认当时间date处理了
			simp1010012.remove("baltime_datelt");
			simp1010012.remove("baltime_datege");
			log.info("逻辑测试4.4:"+map.get("baltime_datege"));
			log.info("逻辑测试4.5:"+map.get("baltime_datelt"));
			queryWrapper=	Condition.getQueryWrapper(simp1010012,Simp1010012.class).between("baltime",map.get("baltime_datege"),map.get("baltime_datelt"));
			log.info("逻辑测试5:"+queryWrapper.getSqlSegment());
			IPage<Simp1010012> pages =simp1010012Service.page(Condition.getPage(query),queryWrapper);
			return R.data(Simp1010012Wrapper.build().pageVO(pages));
		}
		queryWrapper=	Condition.getQueryWrapper(simp1010012,Simp1010012.class);
		log.info("测试7。0："+queryWrapper);
		log.info("测试7："+Condition.getQueryWrapper(simp1010012,Simp1010012.class).getSqlSegment());
		IPage<Simp1010012> pages = simp1010012Service.page(Condition.getPage(query), Condition.getQueryWrapper(simp1010012,Simp1010012.class));
		return R.data(Simp1010012Wrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 消费统计表（月报）
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入simp1010012")
	public R<IPage<Simp1010012VO>> page(Simp1010012VO simp1010012, Query query) {
		IPage<Simp1010012VO> pages = simp1010012Service.selectSimp1010012Page(Condition.getPage(query), simp1010012);
		return R.data(pages);
	}

	/**
	 * 新增 消费统计表（月报）
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入simp1010012")
	public R save(@Valid @RequestBody Simp1010012 simp1010012) {
		return R.status(simp1010012Service.save(simp1010012));
	}

	/**
	 * 修改 消费统计表（月报）
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入simp1010012")
	public R update(@Valid @RequestBody Simp1010012 simp1010012) {
		return R.status(simp1010012Service.updateById(simp1010012));
	}

	/**
	 * 新增或修改 消费统计表（月报）
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入simp1010012")
	public R submit(@Valid @RequestBody Simp1010012 simp1010012) {
		return R.status(simp1010012Service.saveOrUpdate(simp1010012));
	}


	/**
	 * 删除 消费统计表（月报）
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(simp1010012Service.deleteLogic(Func.toLongList(ids)));
	}


}
